package march.test_3_31;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class TestMain1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        Map<Integer,Integer> map = new HashMap<>();
        for(int i = 0; i < n; i++) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            String s = scanner.nextLine();
            if(s.charAt(0) == 'M') {
                if(map.containsKey(a)) {
                    System.out.println(a);
                }
                if(map.containsValue(b)) {
                    System.out.println(b);
                }
            }
        }
    }
}
